//
//  CommunityTableViewCell.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/23.
//

#import "CommunityTableViewCell.h"
#import "CommonUtil.h"
#import "ToolBar.h"
#import <GKPhotoBrowser/GKPhotoBrowser.h>
#import <SDWebImage/SDWebImage.h>

@interface CommunityTableViewCell()

@property (strong, nonatomic) NSMutableArray * imageViews;

@property (strong, nonatomic) UIView * whiteView;
@property (strong, nonatomic) UIImageView * iconImageView;
@property (strong, nonatomic) UILabel * nameLabel;
@property (strong, nonatomic) UILabel * timeLabel;
@property (strong, nonatomic) UILabel * indexLabel;
@property (strong, nonatomic) UILabel * titleLabel;
@property (strong, nonatomic) UIImageView * jingpinImageView;
@property (strong, nonatomic) UILabel * contentLabel;
@property (strong, nonatomic) ToolBar * toolBar;

@end


@implementation CommunityTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.whiteView];
        [self.whiteView addSubview:self.iconImageView];
        [self.whiteView addSubview:self.nameLabel];
        [self.whiteView addSubview:self.timeLabel];
        [self.whiteView addSubview:self.indexLabel];
        [self.whiteView addSubview:self.titleLabel];
        [self.whiteView addSubview:self.jingpinImageView];
        [self.whiteView addSubview:self.contentLabel];
        [self.whiteView addSubview:self.toolBar];

        self.iconImageView.frame = CGRectMake(AD(10), AD(15), AD(40), AD(40));
        self.nameLabel.frame = CGRectMake(self.iconImageView.right + AD(3), AD(20), AD(200), AD(20));
        self.timeLabel.frame = CGRectMake(self.iconImageView.right + AD(3), self.nameLabel.bottom, AD(200), AD(15));
        self.indexLabel.frame = CGRectMake(AD(10), self.iconImageView.bottom + AD(10), AD(40),AD(20));
        self.titleLabel.frame = CGRectMake(self.indexLabel.right + AD(5), self.indexLabel.top, AD(200),AD(20));
        self.jingpinImageView.frame = CGRectMake(AD(200), 0, AD(30), AD(30));
        self.jingpinImageView.centerY = self.titleLabel.centerY;
        self.contentLabel.frame = CGRectMake(AD(10), self.titleLabel.bottom + AD(5), SCREEN_WIDTH-AD(40), 0);
    }
    return self;
}


-(void)setModel:(CommunityModel *)model{
    _model = model;
    self.nameLabel.text =@"这里是昵称";
    self.timeLabel.text =@"2020.12.23 09:30";
    self.indexLabel.text = @"033";
    self.jingpinImageView.hidden = !model.showjingpin;
    self.contentLabel.text = model.content;
    UIFont *font = [UIFont systemFontOfSize:AD(11)];
    CGFloat height = [model.content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT)
    options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
    attributes:@{ NSFontAttributeName : font } context:nil].size.height;
    
    self.contentLabel.height = height;
    

    NSMutableAttributedString *attribut = [[NSMutableAttributedString alloc]initWithString:@"[顶5]这里是内容标题"];
    [attribut addAttribute:NSForegroundColorAttributeName value:RGB(89.0, 146.0, 219.0) range:NSMakeRange(0, 4)];
    self.titleLabel.attributedText = attribut;
    
    for (UIImageView *imagev in self.imageViews) {
        [imagev removeFromSuperview];
    }

    CGFloat margin = AD(10);
    CGFloat w = (SCREEN_WIDTH - (margin * 6))/3;
    CGFloat toolBarY = self.contentLabel.bottom;
    for (NSInteger i = 0; i < model.imageCount; i++) {
        
        if (i < 3 ) {
            UIImageView *imagev = self.imageViews[i];
            imagev.frame = CGRectMake((i * (w + margin)) + margin, self.contentLabel.bottom + AD(10), w, w);
            [imagev sd_setImageWithURL:[NSURL URLWithString:TestImageURL]];
            [self.whiteView addSubview:imagev];
            toolBarY = imagev.bottom;
        }
        
    }
    self.toolBar.top = toolBarY + AD(20);
    self.whiteView.height = self.toolBar.bottom;
}

- (void)imageViewClick:(UITapGestureRecognizer *)tap{
    NSLog(@"%zd",tap.view.tag);
    NSMutableArray *photos = [NSMutableArray array];
    for (NSInteger i = 0; i < self.model.imageCount; i++) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:TestImageURL];
        [photos addObject:photo];
    }

    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:tap.view.tag];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    [browser showFromVC:self.superVC];
}

#pragma mark -lazy

-(UIView *)whiteView{
    if (!_whiteView) {
        _whiteView = [[UIView alloc] initWithFrame:CGRectMake(AD(10), 0, SCREEN_WIDTH-AD(20), 0)];
        _whiteView.backgroundColor = [UIColor whiteColor];
        _whiteView.layer.cornerRadius = AD(5);
        _whiteView.clipsToBounds = YES;
    }
    return _whiteView;
}

-(UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultIcon.jpeg"]];
        _iconImageView.clipsToBounds = YES;
        _iconImageView.layer.cornerRadius = AD(20);
    }
    return _iconImageView;
}

-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont boldSystemFontOfSize:AD(14)];
        _nameLabel.textColor = RGB(255.0, 101.0, 125.0);
    }
    return _nameLabel;
}

-(UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:AD(12)];
        _timeLabel.textColor = [UIColor lightGrayColor];
    }
    return _timeLabel;
}

-(UILabel *)indexLabel{
    if (!_indexLabel) {
        _indexLabel = [[UILabel alloc] init];
        _indexLabel.font = [UIFont systemFontOfSize:AD(12)];
        _indexLabel.textColor = [UIColor whiteColor];
        _indexLabel.backgroundColor = RGB(89.0, 146.0, 219.0);
        _indexLabel.layer.cornerRadius = AD(2);
        _indexLabel.clipsToBounds = YES;
        _indexLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _indexLabel;
}
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:AD(14)];
        _titleLabel.textColor = [UIColor blackColor];
    }
    return _titleLabel;
}

-(UIImageView *)jingpinImageView{
    if (!_jingpinImageView) {
        _jingpinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_jingpin"]];
    }
    return _jingpinImageView;
}
-(UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:AD(11)];
        _contentLabel.textColor = [UIColor lightGrayColor];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

-(NSMutableArray *)imageViews{
    if (!_imageViews) {
        _imageViews = [NSMutableArray array];
        
        for (NSInteger i = 0; i < 3; i++) {
            UIImageView *imageView = [UIImageView new];
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClick:)];
            [imageView addGestureRecognizer:tap];
            [_imageViews addObject:imageView];
        }
        
    }
    return _imageViews;
}

-(ToolBar *)toolBar{
    if (!_toolBar) {
        _toolBar = [[ToolBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-AD(20), AD(50))];
     
    }
    return _toolBar;
}

@end
